import { forEach } from '../utils';

export class InterceptorManager {
    handles = [];

    /**
     * use注册拦截器方法，外部使用
     * @param {*} successful
     * @param {*} failed
     */
    use(successful, failed) {
        this.handles.push({
            successful: successful,
            failed: failed,
        });
        return this.handles.length - 1;
    }

    eject(id) {
        this.handles[id] = null;
        return this;
    }

    /**
     * 调用注册的拦截器
     * @param {*} fn
     */
    foreach(fn) {
        forEach(this.handles, function handleForEach(handle) {
            fn !== null && fn(handle);
        });
    }
}
